package logic

import (
	"context"
	"inventory-system/invenrpc/internal/svc"
	"inventory-system/invenrpc/invenrpc"
	"inventory-system/models"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *invenrpc.UpdateRequest) (*invenrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	goods := models.Goods{
		Model: gorm.Model{
			ID: uint(in.GoodId),
		},
		UserId: in.UserId,
		Name:   in.Name,
		Num:    in.Num,
		Image:  in.Image,
	}
	models.DB.Debug().Where("goodsId = ?", in.GoodId).Updates(&goods)

	return &invenrpc.UpdateResponse{
		GoodId: int64(goods.ID),
	}, nil
}
